
(function(){
	/**
	 * 可以传一个字符串设置一个日期范围，格式为 xxxx-xxxx 或 xxxx xxxx 或 xxxx-xxxx 或 xxxx,xxxx
	 * 如果不传 默认是从2000年到现在
	 * 一个对外设置选中日期的接口 为：setDate()可以设置年月日 分隔符跟上面的一样
	 * 一个对外获取当前选中日期的接口， 为： getDate() 
	 */
	function Calenbar(scope){
		var start, end;
		if(scope !== undefined){
			var temp = scope.split(/,|，|-|\s/);
			if(temp.length == 2 && /^\d+$/.test(temp[0]) && /^\d+$/.test(temp[1])){
				start = temp[0];
				end = temp[1];
			}
		}else{
			start = 2000;
			end = new Date().getFullYear();
		}
		this.scope = {
			start: start,
			end: end
		};
		this.init();
	}
	Calenbar.prototype = {
		constructor: Calenbar,
		now:{
			year: new Date().getFullYear(),
			month: new Date().getMonth()+1,
			day: new Date().getDate()
		},
		flag: false,
		init: function(){
			this.createDom();
			this.render();
			this.change();
		},
		change: function(){
			function year(){
				this.now.year = parseInt(this.yearDom.val());
				this.render();
			}
			function month(){
				this.now.month = parseInt(this.monthDom.val());
				this.render();
			}
			function focus(){
				if(self.flag = !self.flag){
					self.root.css('display','block');
				}else{
					self.root.css('display','none');
				}
				
			}
			var self = this
			this.yearDom.on('change',function(){year.call(self)});
			this.monthDom.on('change',function(){month.call(self)});
			this.choose.on('click',function(){focus.call(self)});
		},
		render: function(){
			if(this.scope.end < this.now.year){
				this.scope.end = this.now.year;
			}
			for(var i = this.scope.start; i <= this.scope.end; i++){
				this.yearDom.append($('<option>'+ i +'年</option>'));
			}
			this.days.html('');

			for(var i = 0; i < this.yearDom.children().length; i++){
				if(this.yearDom.children()[i].innerHTML == this.now.year+'年'){
					this.yearDom.children()[i].selected = true;
				}
			}
			for(var i = 0; i < this.monthDom.children().length; i++){
				if(this.monthDom.children()[i].innerHTML == this.now.month+'月'){
					this.monthDom.children()[i].selected = true;
				}
			}
			this.span = $('<span>1</span>').css({
				'display': 'inline-block',
				'width': '14.2857%',
				'height': '42.84px',
				'background': '#81bdc1',
				'line-height': '42.84px',
				'text-align': 'center',
				'font-weight': 'bold',
				'border-radius': '20%'
			}).on('click',function(self){
				return function(){
					$(this).parent().find('span').css({
						'background': '#81bdc1',
						'color': '#000'
					});
					this.style.background = '#661a1a';
					this.style.color = '#fff';
					self.choose.val(self.getDate());
					self.root.css('display','none');
					self.flag = false;
				}
			}(this));
			switch(new Date(this.now.year+','+this.now.month+','+this.now.day).getDay()){
				case 0:this.days.append(this.span.clone(true).css('margin-left','0px'));break;
				case 1:this.days.append(this.span.clone(true).css('margin-left','14.2857%'));break;
				case 2:this.days.append(this.span.clone(true).css('margin-left',2*14.2857+'%'));break;
				case 3:this.days.append(this.span.clone(true).css('margin-left',3*14.2857+'%'));break;
				case 4:this.days.append(this.span.clone(true).css('margin-left',4*14.2857+'%'));break;
				case 5:this.days.append(this.span.clone(true).css('margin-left',5*14.2857+'%'));break;
				case 6:this.days.append(this.span.clone(true).css('margin-left',6*14.2857+'%'));break;
			}
			var temp;
			switch(this.now.month){
				case 1:
				case 3:
				case 5:
				case 7:
				case 8:
				case 10:
				case 12:
					temp = 31;
					break;
				case 4:
				case 6:
				case 9:
				case 11:
					temp = 30;
					break;
				case 2:
					var temp1 = parseInt(this.yearDom.val());
					if(temp1%4 == 0 && temp1%100 != 0 || temp1%400 == 0){
						temp = 29;
					}else{
						temp = 28;
					}
					break;
			}
			for(i = 2; i <= temp; i++){
				this.days.append(this.span.clone(true).html(i));
			}
			for(i = 0; i < this.days.children().length; i++){
				var a = this.days.children()[i].innerHTML;
				if(this.days.children()[i].innerHTML == this.now.day){
					$(this.days.children()[i]).css('background', '#661a1a').css('color','#fff');
				}
			}
			this.choose.val(this.getDate());
		},
		createDom: function(){
			this.main = $('<div>')
				.css({
					'border':'1px solid #ccc',
					'width': '297px',
					'margin': '20px auto'
				})
				.appendTo($('body'));
			this.choose = $('<input>').css({
				'width': '100%',
				'height': '25px',
				'padding-left': '10px',
				'box-sizing': 'border-box'
			}).appendTo(this.main);
			this.root = $('<div>').css('border-radius','5px').css('background', '#b4e6b2');
			this.root.css({
				'border':'1px solid #ccc',
				'width': '300px',
				'margin': '0 auto',
				'display': 'none'
			}).appendTo(this.main);
			this.yearDom = $('<select>').css({
				'width': '45%',
				'height': '25px',
				'margin': '5px'
			});
			this.monthDom = $('<select>').css({
				'width': '45%',
				'height': '25px',
				'margin': '5px'
			});
			
			for(i = 1; i <= 12; i++){
				this.monthDom.append($('<option>' + i + '月</option>'));
			}
			this.weekDom = $('<div>').css('width','100%');
			for(i = 0; i < 7; i++){
				var temp = '';
				switch(i){
					case 0:temp = '日';break;
					case 1:temp = '一';break;
					case 2:temp = '二';break;
					case 3:temp = '三';break;
					case 4:temp = '四';break;
					case 5:temp = '五';break;
					case 6:temp = '六';break;
				}
				this.weekDom.append($('<span>').css({
					'display': 'inline-block',
					'width':1/7*100 + '%',
					'height': '42.84px',
					'text-align': 'center',
					'background': '#c1d142',
					'line-height': '42.84px'
				}).html(temp));
			}
			this.weekDom.children().first().css('background','#b74292');
			this.weekDom.children().last().css('background','#b74292');
			this.days = $('<div>').css('width','100%');
			this.root.append($('<div>').css({
				'width': '98%',
				'height': '35px',
				'margin': '3px auto',
				'background': '#7d6b6b',
				'border-radius': '5px',
				'text-align': 'center'
			}).append(this.yearDom).append(this.monthDom))
			.append(this.weekDom)
			.append(this.days);
		},
		setDate: function(day){
			if(day !== undefined){
				var temp = day.split(/,|，|-|\s/);
				if(/^\d+$/.test(temp[0]) && /^\d+$/.test(temp[1]) && /^\d+$/.test(temp[2])){
					this.now.year = parseInt(temp[0]);
					this.now.month = parseInt(temp[1]);
					this.now.day = parseInt(temp[2]);
					this.render();
				}
			}
		},
		getDate: function(){
			for(var i = 0; i < this.days.children().length; i++){
				if(this.days.children().eq(i).css('color') == 'rgb(255, 255, 255)'){
					var day = this.days.children().eq(i).text();
				}
			}
			return this.now.year + '-' + this.now.month + '-' + day;
		}
	};
	window.Calenbar = Calenbar;
})();